﻿
package com.STS.ELearning.Captivate.helpers {
	
	
	public class CaptivateHelper{
		
		public static function createObjectFromXmlData(xml:XML):Object
		{
			var returnObj:Object = new Object();
			
			for (var i:int = 0; i < XMLList(xml.property).length(); i++) {
				
			
				var propName:String = xml.property[i].@id;
			
				var dataType:String = xml.property[i].child(0).name();
			
				var thisProperty:XMLList = XMLList(xml.property[i]);
				
				switch (dataType) { 
					
					
					case "number" :
					returnObj[propName] = translateNumber(thisProperty,true);
					break;
					
					case "string" :
					returnObj[propName] = translateString(thisProperty, true);
					break;
					
					
					case "array" :
					returnObj[propName] = translateArray(thisProperty.child(0));
					break;
					
					case "object" :
					for each (var property:XML in thisProperty.children()) {
						returnObj[propName] = createObjectFromXmlData(property); 
						break;
					}
					break;
					
					
					case "true" :
					returnObj[propName] = true;
					break;
					
					case "false" :
					returnObj[propName] = false;
					break;
					
					case "null" :
					returnObj[propName] = null;
					break;
					
					default :
					returnObj[propName] = ifAllElseFails(thisProperty, true); 
					break;
				}
			}
			
			return returnObj;
		}
		
		static private function translateString(xml:XMLList, fromObject:Boolean):String
		{

			if (fromObject) { 
				return String(xml.child(0).child(0));
			} else { 
				return String(xml.child(0));
			}
		}
		

		private static function translateArray(xml:XMLList):Array
		{
			
			var returnArray:Array = new Array();
			
			
			for (var i:int = 0; i < XMLList(xml.property).length(); i++) {
				
			
				var dataType:String = xml.property[i].child(0).name();
			
				var thisProperty:XMLList = XMLList(xml.property[i]);
				
				switch (dataType) { 
					case "number" :
					returnArray.push(translateNumber(thisProperty,false));
					break;
					
					case "string" :
					returnArray.push(translateString(thisProperty, false));
					break;
					
					case "array" :
					returnArray.push(translateArray(thisProperty.child(0))); 
					break;
					
					case "object" :
					for each (var property:XML in thisProperty.children()) {
						
						returnArray.push(createObjectFromXmlData(property));
					}
					break;
					
					
					case "true" :
					returnArray.push(true);
					break;
					
					case "false" :
					returnArray.push(false);
					break;
					
					case "null" :
					returnArray.push(null);
					break;
					
					
					default :
					returnArray.push(ifAllElseFails(thisProperty, false));
					break;
					
				}
				
			}
			
			return returnArray;
		}
		
		
		private static function translateNumber(xml:XMLList, fromObject:Boolean):Number
		{
			if (fromObject) { 
				return Number(xml.child(0).child(0));
			} else { 
				return Number(xml.child(0));
			}
		}
		private static function ifAllElseFails(xml:XMLList, fromObject:Boolean):XMLList
		{
			if (fromObject) { 
				return xml.child(0).child(0);
			} else { 
				return xml.child(0);
			}
			
		}
		
	}

}